package com.yihu.cms.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.yihu.beans.Derivatives;
import com.yihu.beans.searchCondition.DerivativesPageConditions;
import com.yihu.cms.service.DerivativesService;


@Controller
public class DerivativesController extends BaseController {
	
	protected final Logger log = LoggerFactory.getLogger(this.getClass());

	@RequestMapping(value = "/derivatives", method = RequestMethod.POST)
	public @ResponseBody Object create(@Valid @RequestBody Derivatives derivatives , BindingResult vResult) {
		log.info("create derivatives .");
		if (vResult.hasErrors()){
			List<ObjectError> errorList = vResult.getAllErrors();
            for(ObjectError error : errorList){
                log.error("validate data error . error message = " + error.getDefaultMessage());
            }
			return getResult(1);
		}
		DerivativesService ds = new DerivativesService();
		int result = ds.add(derivatives);
		return getResult(result, null);
	}

	@RequestMapping(value = "/derivatives", method = RequestMethod.PUT)
	public @ResponseBody Object update(@Valid @RequestBody Derivatives derivatives , BindingResult vResult) {
		DerivativesService ds = new DerivativesService();
		int result = ds.update(derivatives);
		return getResult(result, null);
	}

	@RequestMapping(value = "/derivatives/{id}", method = RequestMethod.DELETE)
	public @ResponseBody Object delete(@PathVariable("id") String id) {
		// logger.info("删除人员信息id=" + id);
		DerivativesService ds = new DerivativesService();
		int result = ds.del(id);
		return getResult(result, null);
	}

	@RequestMapping(value = "/derivatives/{id}", method = RequestMethod.GET)
	public @ResponseBody Derivatives getById(@PathVariable("id") String id) {
		DerivativesService ds = new DerivativesService();
//		String derivativesJson = ds.getById(id);
		return ds.getById(id);
	}
	
//	@RequestMapping(value = "/derivatives/main")
//	// , method = RequestMethod.PATCH
//	public @ResponseBody Map<String , Object> getPaging(int pageSize) {
//		//@RequestParam(value = "name", required = false, defaultValue = "")
//		DerivativesService ds = new DerivativesService();
//		List<Derivatives> list = ds.getDerivativesPaging(1, pageSize);
//		Map<String , Object> map = getResult(0 , list);
//		long total = ds.getDerivativesTotal();
//		map.put("total", total);
//		return map;
//	}
	
	@RequestMapping(value = "/derivatives/list" , method = RequestMethod.POST)
	// , method = RequestMethod.PATCH
	public @ResponseBody Map<String , Object> getPaging(
			@RequestParam(value = "curPage", required = false, defaultValue = "1") int curPage,
			@RequestParam(value = "pageSize", required = false, defaultValue = "5") int pageSize,
			@RequestParam(value = "commodityCategoryId", required = false, defaultValue = "") String commodityCategoryId,
			@RequestBody DerivativesPageConditions pc) {
		//@RequestParam(value = "name", required = false, defaultValue = "")
//		PageConditions pc = new PageConditions();
//		pc.setCategoryId(category);
//		pc.setCommodityStatesId(states);
//		pc.setKeyword(keyword);
		if(!"".equals(commodityCategoryId))
		{
			pc.setCommodityCategoryId(commodityCategoryId);
		}
		DerivativesService ds = new DerivativesService();
		List<Derivatives> list = ds.getPaging(curPage , pageSize , pc);
		Map<String , Object> map = getResult(0 , list);
		long total = ds.getTotal();
		map.put("total", total);
		return map;
	}

//	@RequestMapping(value = "/derivatives/list")
//	// , method = RequestMethod.PATCH
//	public @ResponseBody List<Derivatives> getPaging(int curPage , int pageSize) {
//		//@RequestParam(value = "name", required = false, defaultValue = "")
//		DerivativesService ds = new DerivativesService();
//		List<Derivatives> list = ds.getDerivativesPaging(curPage, pageSize);;
//		return list;
//	}

	public static void main(String args[]) {
		DerivativesController dc = new DerivativesController();
		Map<String, Object> result = dc.getResult(1 , null);
		System.out.println("result = " + JSON.toJSONString(result));
	}



}
